/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Registro.java
 *
 * Created on 25-ago-2012, 16:33:51
 */
package Presentacion;

import Logica.RmiServer;
import Logica.Tablero;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 * Clase encargada del registro de los jugadores a la partida
 * @author jero
 */
public class Registro extends javax.swing.JFrame {

    /** Creates new form Registro */
    
    InterfaceRemota rmiServer;
   
    /**
     * Constructor de la clase
     */
    public Registro() {
        this.getContentPane().setBackground(Color.BLACK);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();
        frameSize.height = ((frameSize.height > screenSize.height) ? screenSize.height : frameSize.height);
        frameSize.width = ((frameSize.width > screenSize.width) ? screenSize.width : frameSize.width);
        this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);         
        
        initComponents();
        this.setSize(600, 400);
        jRadioButton1.setActionCommand("Batman");
        jRadioButton2.setActionCommand("Robin");
        jRadioButton3.setActionCommand("Joker");
        jRadioButton4.setActionCommand("Riddle");
        buttonGroup1.add(jRadioButton1);
        buttonGroup1.add(jRadioButton2);
        buttonGroup1.add(jRadioButton3);
        buttonGroup1.add(jRadioButton4);              
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel3 = new javax.swing.JLabel();
        textNombre = new javax.swing.JTextField();
        registro = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jMenu5 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SCRABBLE Batman - REGISTRO");
        setBackground(new java.awt.Color(255, 255, 255));
        setBounds(new java.awt.Rectangle(15, 15, 0, 0));
        getContentPane().setLayout(null);

        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Nombre:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(120, 140, 80, 14);
        getContentPane().add(textNombre);
        textNombre.setBounds(180, 140, 160, 20);

        registro.setText("Registrar");
        registro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registroActionPerformed(evt);
            }
        });
        getContentPane().add(registro);
        registro.setBounds(180, 290, 110, 23);

        jLabel7.setIcon(new javax.swing.ImageIcon("C:\\Imagenes\\ScrabbleLogo.png")); // NOI18N
        getContentPane().add(jLabel7);
        jLabel7.setBounds(20, 20, 190, 60);

        jLabel2.setIcon(new javax.swing.ImageIcon("C:\\Imagenes\\Batman.png")); // NOI18N
        getContentPane().add(jLabel2);
        jLabel2.setBounds(130, 70, 150, 40);

        jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Imagenes\\batmanLogo.gif")); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(210, 10, 100, 60);

        jRadioButton1.setBackground(new java.awt.Color(0, 0, 0));
        jRadioButton1.setForeground(new java.awt.Color(204, 204, 255));
        jRadioButton1.setText("Batman");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jRadioButton1);
        jRadioButton1.setBounds(150, 190, 90, 23);

        jRadioButton2.setBackground(new java.awt.Color(0, 0, 0));
        jRadioButton2.setForeground(new java.awt.Color(255, 255, 102));
        jRadioButton2.setText("Robin");
        getContentPane().add(jRadioButton2);
        jRadioButton2.setBounds(150, 240, 90, 23);

        jRadioButton3.setBackground(new java.awt.Color(0, 0, 0));
        jRadioButton3.setForeground(new java.awt.Color(255, 51, 204));
        jRadioButton3.setText("Joker");
        getContentPane().add(jRadioButton3);
        jRadioButton3.setBounds(280, 190, 90, 23);

        jRadioButton4.setBackground(new java.awt.Color(0, 0, 0));
        jRadioButton4.setForeground(new java.awt.Color(51, 255, 51));
        jRadioButton4.setText("Riddler");
        getContentPane().add(jRadioButton4);
        jRadioButton4.setBounds(280, 240, 100, 23);

        jLabel5.setIcon(new javax.swing.ImageIcon("C:\\Imagenes\\BatmanIcon.png")); // NOI18N
        getContentPane().add(jLabel5);
        jLabel5.setBounds(110, 170, 35, 50);

        jLabel6.setIcon(new javax.swing.ImageIcon("C:\\Imagenes\\jokerIcon.png")); // NOI18N
        getContentPane().add(jLabel6);
        jLabel6.setBounds(240, 170, 33, 50);

        jLabel8.setIcon(new javax.swing.ImageIcon("C:\\Imagenes\\robinIcon.png")); // NOI18N
        getContentPane().add(jLabel8);
        jLabel8.setBounds(110, 220, 32, 50);

        jLabel4.setIcon(new javax.swing.ImageIcon("C:\\Imagenes\\riddleIcon.png")); // NOI18N
        getContentPane().add(jLabel4);
        jLabel4.setBounds(240, 220, 32, 50);

        jMenu1.setText("Archivo");

        jMenu3.setText("Salir");
        jMenu1.add(jMenu3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Ayuda");

        jMenu4.setText("Reglas del Juego");
        jMenu2.add(jMenu4);

        jMenu5.setText("Acerca de Scrabble Batman");
        jMenu2.add(jMenu5);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents
/**
 * Asigna la acción de agregar un jugador a la partida al botón "Registrar"
 * @param evt 
 */
private void registroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registroActionPerformed
        
        try {
            
            int numeroJugadores = RmiServer.getInstance().getRmiServer().getTablero().getJugadores().size();
            if(numeroJugadores < 4){
            // TODO add your handling code here:
               //boolean confirmacion = rmiServer.crearJugador(textNombre.getText());
               System.out.println(buttonGroup1.getSelection().getActionCommand());
               int idJugador = RmiServer.getInstance().getRmiServer().crearJugador(textNombre.getText(), buttonGroup1.getSelection().getActionCommand());
               
               esperarJugadores();
               
               Principal principal = new Principal(idJugador);
               principal.setVisible(true);
               principal.getInterfaceRemota(rmiServer); 
               this.setVisible(false);
               //principal.getInterfaceRemota(RmiServer.getInstance().getRmiServer());
               //principal.pintarTablero();              
            }
            else{
               JOptionPane.showMessageDialog(null, "Lo sentimos, el numero de participantes ha excedido el limite");
            
            }
        } catch (RemoteException ex) {
            Logger.getLogger(Registro.class.getName()).log(Level.SEVERE, null, ex);
        }
      
      
}//GEN-LAST:event_registroActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // TODO add your handling code here:

    }//GEN-LAST:event_jRadioButton1ActionPerformed

/*
 * Asigna el valor recibido por parámetro al atributo InterfaceRemota rmiServer
 */
public void getInterfaceRemota(InterfaceRemota ir){
   
    rmiServer = ir;

}

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JButton registro;
    private javax.swing.JTextField textNombre;
    // End of variables declaration//GEN-END:variables

    
    /*
     * Verifica que el número de jugadores de la partida sea mayor a 1
     */
    private void esperarJugadores() throws RemoteException {
        
        boolean empezarPartida = true;
        registro.setEnabled(false);
        JOptionPane.showMessageDialog(null, "Esperando a que se unan los demas participantes");
        
        //Espera a que el numero permitido de jugadores este registrado para empezar la partida
        while(empezarPartida){   
            
            if(RmiServer.getInstance().getRmiServer().getTablero().getJugadores().size() == 2){
               empezarPartida = false;
            }             
        }
    }
    
}
